package cybertechcebu.task
{
	import cybertechcebu.FTPClient;
	import cybertechcebu.FTPCommand;
	import cybertechcebu.FTPResponse;
	import cybertechcebu.enum.Commands;
	import cybertechcebu.enum.Responses;

	public class LoginTask extends Task
	{
		private var username:String;
		private var password:String;
		private var userSent:Boolean = false;

		public function LoginTask(client:FTPClient, username:String, password:String)
		{
			super(client);
			this.username = username;
			this.password = password;
		}
		
		override protected function startTask():void{}
		
		
		override public function handleResponse(response:FTPResponse):void
		{
			switch (response.code)
			{
				case Responses.SERVICE_READY:
					if(!userSent){
						sendCommand(new FTPCommand(Commands.USER, username));
						userSent = true;
					}
					break;
				case Responses.USER_OK:
					sendPassword(new FTPCommand(Commands.PASS, password));
					break;
				case Responses.LOGGED_IN:					
					dispatchTaskProgress("Logged in as '" + username + "'");
					client.loggedIn = true;
					dispatchTaskEnd(true);
					break;
				case Responses.DATA_CONN_CLOSE:
					break;
				default:
					dispatchTaskError("Unhandled response code: " + response.code);
			}
		}

	}
}
